package cn.itsource.ymcc.service.impl;

import cn.itsource.ymcc.domain.KillActivity;
import cn.itsource.ymcc.domain.KillCourse;
import cn.itsource.ymcc.mapper.KillActivityMapper;
import cn.itsource.ymcc.service.IKillActivityService;
import cn.itsource.ymcc.service.IKillCourseService;
import cn.itsource.ymcc.util.AssertUtil;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RSemaphore;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author whale.chen
 * @since 2022-08-04
 */
@Service
@Slf4j
public class KillActivityServiceImpl extends ServiceImpl<KillActivityMapper, KillActivity> implements IKillActivityService {
    @Autowired
    private IKillCourseService killCourseService;
    @Autowired
    private RedisTemplate<Object,Object> redisTemplate;

    @Autowired
    private RedissonClient redissonClient;


    @Override
    public void publish(Long activityId) {
        //修改成上架状态 发布
    //1.查询出秒杀活动对应的课程
        List<KillCourse> killCourses=queryActivityCourse(activityId);
        KillCourse killCourse = killCourses.get(0);
        //* 2.校验时间是否满足，满足条件才发布活动
        Date startTime = killCourse.getStartTime();
        Date now =new Date();
        boolean before = now.before(killCourse.getEndTime());
        AssertUtil.isTrue(before,"时间不对呀");
        //* 3.将活动下面的所有课程存储redis  怎么存？
        killCourses.forEach(course->{
            //设置信号量
            String key = activityId +":"+course.getId();
            //获取唯一的信号量对象
            RSemaphore semaphore = redissonClient.getSemaphore(key);
            //设置秒杀库存数量
            boolean setPermits = semaphore.trySetPermits(course.getKillCount());
            if(setPermits){//设置库存成功，在添加课程到redis
                // 4.将活动下面的所有课程存储redis  怎么存？
                log.info("添加秒杀课程成功！{}",course);
                // 要存进redis  里面必须是string 类型 为了更好地获取，加一个前缀
                redisTemplate.opsForHash().put("activity:"+activityId,course.getId().toString(),course);
                course.setPublishStatus(1);
                course.setPublishTime(now);
                killCourseService.updateById(course);
            }else {
                log.info("添加秒杀课程失败！{}", course);

            }
        });
        //查询出所有活动
        KillActivity killActivity = selectById(activityId);
        killActivity.setPublishStatus(1);
        killActivity.setPublishTime(now);
        //修改一些状态
        updateById(killActivity);

    }

    private List<KillCourse> queryActivityCourse(Long activityId) {
        Wrapper<KillCourse> query = new EntityWrapper<>();
        query.eq("activity_id",activityId);
        return killCourseService.selectList(query);
    }


}
